/**
 * 
 */
package test;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import edu.uci.ics.crawler4j.crawler.Page;

/**
 * 
 * Handles the html page
 * 
 * @author Sinisa
 * 
 */
class PageHandler {

        private static final Logger LOG = Logger.getLogger(PageHandler.class);

        /**
         * @param page
         *                the html page to be handled
         */
        void handlePage(Page page) {
                String url = page.getWebURL().getURL();
                // byte[] pg = page.getHTML().getBytes("UTF-16");
                Document document = Jsoup.parse(page.getHTML());
                Element content = document.getElementById("Results");

                Element companyName = content.select("a[class=fn org url]").first();
                Element streetAddress = content.getElementsByClass("street-address").first();
                Element locality = content.getElementsByClass("locality").first();
                Element latitude = content.getElementsByClass("latitude").first();
                Element longitude = content.getElementsByClass("longitude").first();
                Element tel = content.getElementsByClass("tel").first();
                Element classification = content.getElementsByClass("classification").first();
                Element orderRightCorner = content.getElementsByClass("order-right-corner").first();
                Element serviceLinks = content.getElementsByClass("ServiceLinks").first();

                CompanyInfo info = new CompanyInfo();
                if (companyName != null)
                        info.setFnOrgUrl(companyName.text());
                if (classification != null)
                        info.setClassification(classification.text());
                if (latitude != null)
                        info.setLatitude(latitude.text());
                if (locality != null)
                        info.setLocality(locality.text());
                if (longitude != null)
                        info.setLongitude(longitude.text());
                if (orderRightCorner != null)
                        info.setOrderRightCorner(orderRightCorner.text());
                if (serviceLinks != null)
                        info.setServiceLinks(serviceLinks.html());
                if (streetAddress != null)
                        info.setStreetAddress(streetAddress.text());
                if (tel != null)
                        info.setTel(tel.text());
                if (url != null)
                        info.setUrl(url);
                LOG.info(info);

                ObjectPersister persister = new ObjectPersister();
                persister.save(info);
        }

        private void deleteThis(String url) {
                String[] str = url.split("zk.com.mk/Pages/");
                if (str != null && str.length >= 1) {
                        String secondPart = str[1];
                        if (secondPart != null) {
                                String[] splitSecondPart = secondPart.split("/");
                                if (splitSecondPart != null && splitSecondPart.length >= 1) {
                                        String fileName = splitSecondPart[0] + "." + splitSecondPart[1];
                                        if (fileName != null && !fileName.isEmpty()) {
                                                System.out.println(fileName);
                                                File file = new File("/zkData/" + fileName + ".html");
                                                if (file != null && file.exists()) {
                                                        System.out.println("File " + fileName + " exists");
                                                        return;
                                                } else {
                                                        System.out.println("File " + fileName + " doesnt exist");
                                                }

                                        }
                                } else {
                                        LOG.debug("not valid second part");
                                }
                        } else {
                                LOG.debug("empty second part of url");
                        }
                } else {
                        LOG.debug("url not valid: " + url);
                }
        }

}
